package com.train.mgr.modules.biz.controller;

import com.train.common.model.PageParam;
import com.train.common.model.PageResult;
import com.train.common.model.R;
import com.train.mgr.config.poi.ExcelUtil;
import com.train.mgr.modules.biz.dto.request.*;
import com.train.mgr.modules.biz.dto.response.EmployeeRespDTO;
import com.train.mgr.modules.system.dto.response.UserRespDTO;
import com.train.mgr.modules.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@Api( tags = "企业员工[企业端]")
@RequestMapping("/v1/employee")
public class EmployeeController {

    private final UserService userService;

    @PostMapping("/add")
    @ApiOperation("新增企业员工")
    public R<Boolean> add(@RequestBody @Validated EmployeeReqDTO body) {
        return R.succeed(userService.addEmployee(body) >0);
    }

    @PostMapping("/check_name")
    @ApiOperation("验证账号唯一")
    public R<Boolean> checkName(@RequestBody @Validated CheckEmpReqDTO body) {
        return R.succeed(userService.checkName(body) );
    }


    @PostMapping("/edit")
    @ApiOperation("编辑企业员工")
    public R<Boolean> edit(@RequestBody @Validated EmployeeReqDTO body) {
        return R.succeed(userService.updateEmployee(body) >0);
    }

    @GetMapping("{id}")
    @ApiOperation("查询员工详情")
    public R<UserRespDTO> get(@PathVariable("id") Long id) {
        return R.succeed(userService.getEmpUserById(id));
    }

    @PostMapping("/page")
    @ApiOperation("企业员工列表")
    public R<PageResult<EmployeeRespDTO>> page(@RequestBody EmpQueryReqDTO params) {
        return R.succeed(userService.pageEmpList(params));
    }

    @PostMapping("/check_emp_list")
    @ApiOperation("待审核员工列表")
    public R<PageResult<EmployeeRespDTO>> checkEmployeeList(@RequestBody PageParam params) {
        return R.succeed(userService.checkEmployeeList(params));
    }


    @PostMapping("/batch_emp_check")
    @ApiOperation("批量审核通过和不通过")
    public R<Boolean> batchEmpCheck(@RequestBody @Validated BatchIdsDTO body) {
        Boolean result = userService.batchEmpCheck(body);
        return R.succeed(result);
    }


    @PostMapping("/part_emp_list")
    @ApiOperation("企业部分员工（过滤课程员工）")
    public R<PageResult<EmployeeRespDTO>> partEmpList(@RequestBody EmpQueryReqDTO params) {
        return R.succeed(userService.findNotCourseUserList(params));
    }

    @ApiOperation("导入验证员工")
    @PostMapping("/import_check")
    public R<List<EmpImportDTO>> importCheckExcel(@RequestParam("file") MultipartFile file) {
        ExcelUtil<EmpImportDTO> util = new ExcelUtil<>(EmpImportDTO.class);
        List<EmpImportDTO> importList=null;
        try {
            importList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            throw new RuntimeException("excel导入客户数据解析异常，请联系管理员");
        }
        //log.info("导入员工 List:{}", JSONObject.toJSONString(importList));
        return R.succeed(userService.checkExcelData(importList));
    }


    @ApiOperation("导入保存员工")
    @PostMapping("/import_save")
    public R<Boolean> importSaveExcel(@RequestParam("file") MultipartFile file) {
        ExcelUtil<EmpImportDTO> util = new ExcelUtil<>(EmpImportDTO.class);
        List<EmpImportDTO> importList=null;
        try {
            importList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            throw new RuntimeException("excel导入客户数据解析异常，请联系管理员");
        }
        //log.info("导入员工 List:{}", JSONObject.toJSONString(importList));
        userService.saveExcelData(importList);
        return R.succeed("导入数据成功~");
    }
}
